package com.learnyou.server.xingzuo.common.helper;

import java.util.LinkedList;

public class FixCapacityQueue<T> {
    private int capacity;
    private LinkedList<T> linkedList;

    public FixCapacityQueue(int capacity) {
        this.capacity = capacity;
        this.linkedList = new LinkedList<>();
    }

    public void add(T e){
        this.linkedList.add(e);
        if (this.linkedList.size() > capacity){
            this.linkedList.poll();
        }
    }

    public T get(int index){
        if (index > this.linkedList.size()-1){
            return null;
        }
        return this.linkedList.get(index);
    }

    public T poll(){
        return this.linkedList.pollLast();
    }

    public boolean isNotEmpty() {
        return !this.linkedList.isEmpty();
    }
}
